using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.Data.Readers;

namespace TrainingManagement.api.Models.Data.CRUD.Position
{
    public class PositionSelectDataSource : QuerySource<int?, List<BusinessObjects.Position>>
    {
        public override List<BusinessObjects.Position> Execute(int? request)
        {
            const string selectPosition = "dbo.training_api_PositionSelect";
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@PositionID", SqlDbType.Int){Value = request};

            using (var rdr = SqlHelper.ExecuteReader(
                ConnectionStrings.TrainingDb,
                CommandType.StoredProcedure,
                selectPosition,
                parameters))
            {
                var result = new List<BusinessObjects.Position>();
                var positionReader = new PositionDataReader();
                while (rdr.Read())
                {
                    result.Add(positionReader.Run(rdr));
                }
                return result;
            }
        }
    }
}